package com.threeonemeeting.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.threeonemeeting.R;
import com.threeonemeeting.listcache.MeetingCommentInfoCache;
import com.threeonemeeting.model.MeetingCommentListItemInfo;
import com.threeonemeeting.util.MiscUtil;

public class MeetingCommentInfoListAdapter extends
		ArrayAdapter<MeetingCommentListItemInfo> {

	private ArrayList<MeetingCommentListItemInfo> mCommentsList;

	public MeetingCommentInfoListAdapter(Activity activity) {
		super(activity, 0);
	}

	public void setCommentList(
			ArrayList<MeetingCommentListItemInfo> commentsList) {
		mCommentsList = commentsList;
		notifyDataSetChanged();
	}

	public ArrayList<MeetingCommentListItemInfo> getCommentsList() {
		return mCommentsList;
	}

	public void add(MeetingCommentListItemInfo commentsInfo) {
		if (mCommentsList == null)
			mCommentsList = new ArrayList<MeetingCommentListItemInfo>();

		mCommentsList.add(commentsInfo);
	}

	public void clear() {
		if (mCommentsList != null) {
			mCommentsList.clear();
		}
	}

	@Override
	public int getCount() {
		if (mCommentsList == null)
			return 0;

		return mCommentsList.size();
	}

	@Override
	public MeetingCommentListItemInfo getItem(int position) {
		return mCommentsList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		Activity activity = (Activity) getContext();
		MeetingCommentListItemInfo commentInfo = getItem(position);

		View rowView = convertView;
		MeetingCommentInfoCache viewCache = null;

		if (rowView == null) {
			LayoutInflater inflater = activity.getLayoutInflater();
			rowView = inflater.inflate(R.layout.lvi_meeting_comment_item, null);
			viewCache = new MeetingCommentInfoCache(rowView, 0, null,
					commentInfo.strCommentName, commentInfo.strCommentTime,
					commentInfo.strCommentText);
			viewCache.setRepliedCommentName(commentInfo.strRepliedCommentName);
			viewCache.setRepliedCommentTime(commentInfo.strRepliedCommentTime);
			viewCache.setRepliedCommentMsg(commentInfo.strRepliedCommentText);
			viewCache.setHasCommentImage(true);

			rowView.setTag(viewCache);
		} else {
			viewCache = (MeetingCommentInfoCache) rowView.getTag();
		}

		// 评论者的头像
		String strImageUrl = "http://file.31huiyi.com/UserFaceHandler.ashx?uid="
				+ commentInfo.nCommentUserId;
		ImageView commentHeaderImageView = viewCache
				.getCommentHeaderImageView();
		MiscUtil.setImageView(commentHeaderImageView, strImageUrl);

		// 评论的人
		TextView commentNameView = viewCache.getCommentNameTextView();
		commentNameView.setText(commentInfo.strCommentName);

		// 评论的时间
		TextView commentTimeView = viewCache.getCommentTimeTextView();
		commentTimeView.setText(commentInfo.strCommentTime);

		// 评论的内容
		TextView commentMsgView = viewCache.getCommentMsgTextView();
		commentMsgView.setText(commentInfo.strCommentText);

		LinearLayout repliedLayout = viewCache.getRepliedLayout();
		if (commentInfo.bReplied == true) {
			repliedLayout.setVisibility(View.VISIBLE);

			// 被评论的人
			TextView repliedCommentNameView = viewCache
					.getRepliedCommentNameTextView();
			repliedCommentNameView.setText(commentInfo.strRepliedCommentName);

			// 被评论的时间
			TextView repliedCommentTimeView = viewCache
					.getRepliedCommentTimeTextView();
			repliedCommentTimeView.setText(commentInfo.strRepliedCommentTime);

			// 被评论的内容
			TextView repliedCommentMsgView = viewCache
					.getRepliedCommentMsgTextView();
			repliedCommentMsgView.setText(commentInfo.strRepliedCommentText);
		} else {
			repliedLayout.setVisibility(View.GONE);
		}

		ImageView imageView = viewCache.getImageView();
		if (commentInfo.bIsHaveImage == true) {
			imageView.setVisibility(View.VISIBLE);
		} else {
			imageView.setVisibility(View.GONE);
		}

		// 是否包含图片
		TextView repliedNumTextView = viewCache.getRepliedNumTextView();
		if (commentInfo.nReplyNum > 0) {
			repliedNumTextView.setText("回复(" + commentInfo.nReplyNum + ")");
		}

		return rowView;
	}
}
